/*
 * @Author: leechoy 1419352779@qq.com
 * @Date: 2025-01-13 12:56:41
 * @LastEditors: leechoy 1419352779@qq.com
 * @LastEditTime: 2025-01-14 11:29:36
 * @FilePath: /miniClient/pages/index/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    pendingSign: 0,
    pendingOthers: 0,
    completed: 0,
    recentContracts: []
  },

  onLoad() {
    this.getContractStats()
    this.getRecentContracts()
  },

  onShow() {
    // 每次显示页面时刷新数据
    this.getContractStats()
    this.getRecentContracts()
  },

  // 获取合同统计数据
  getContractStats() {
    // TODO: 调用后端接口获取数据
    wx.request({
      url: 'your-api-url/contract/stats',
      success: (res) => {
        this.setData({
          pendingSign: res.data.pendingSign,
          pendingOthers: res.data.pendingOthers,
          completed: res.data.completed
        })
      }
    })
  },

  // 获取最近合同列表
  getRecentContracts() {
    // TODO: 调用后端接口获取数据
    wx.request({
      url: 'your-api-url/contract/recent',
      success: (res) => {
        this.setData({
          recentContracts: res.data.list
        })
      }
    })
  },

  // 跳转到合同列表
  navigateToList() {
    wx.switchTab({
      url: '/pages/contract/list'
    })
  },

  // 创建合同
  createContract() {
    wx.navigateTo({
      url: '/pages/contract/create'
    })
  },

  // 扫码签署
  scanContract() {
    wx.scanCode({
      success: (res) => {
        // 处理扫码结果，跳转到签署页面
        wx.navigateTo({
          url: `/pages/contract/sign?id=${res.result}`
        })
      }
    })
  },

  // 使用模板
  useTemplate() {
    wx.navigateTo({
      url: '/pages/template/list'
    })
  },

  // 查看合同详情
  goToDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/contract/detail?id=${id}`
    })
  }
})
